#include <iostream>
#include <string>
#include <stdio.h>
#include <stdlib.h>
#include "utils/Logger.h"
#include "zip/ZipFile.h"
#include "utils/fileHandle.h"

int main(int argc, char const *argv[])
{
	if (argc < 3) {
		log_err("argc err!");
		return -1;
	}
	std::string source(argv[1]), target(argv[2]);
	//TODO 解压文件
    ZipFile zf;

    status_t ret = zf.open(source.c_str(),ZipFile::kOpenReadOnly);
    if (NO_ERROR != ret) {
        log_err("[DataBdFinished::doAction] open %s fail\n",source.c_str());
    }else{
        int32_t entryNum = zf.getNumEntries();
        log_debug("[DataBdFinished::doAction] num entries %#x\n",entryNum);
        for (int32_t idx = 0; idx < entryNum; idx++) {
            ZipEntry* entry = zf.getEntryByIndex(idx);
            std::string entryName(entry->getFileName());
            entryName = target + "/" + entryName;

            log_debug("[DataBdFinished::doAction] unzip %s\n",entryName.c_str());

            void* buf = zf.uncompress(entry);
            int32_t length = entry->getUncompressedLen();
            linuxSys::save_file_without_sync(entryName.c_str(),static_cast<char*>(buf),length);
            free(buf);
        }
    }
	return 0;
}